<template>
  <div class="all">
    <div class="main">
      <!-- 头部 -->
      <el-row :gutter="20">
        <el-col :span="4"
          ><el-input v-model="input" placeholder="请输入内容"></el-input
        ></el-col>
        <el-col :span="2"><el-button @click="clearselect">清空</el-button></el-col>
        <el-col :span="15"><el-button type="primary" @click="selectPermissions">搜索</el-button></el-col>
        <el-col :span="2"
          ><el-button type="success" class="el-icon-edit" @click="openAdd"
            >新增权限组</el-button
          ></el-col
        >
      </el-row>
      <!-- 分隔线 -->
      <div class="separate">
        <p><span class="el-icon-info"></span> 共{{ total }}条记录</p>
      </div>
      <!-- 表格 -->
      <el-table
        ref="filterTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="selection" width="250"> </el-table-column>
        <el-table-column prop="title" label="用户名" width="250">
        </el-table-column>
        <el-table-column
          prop="create_date"
          label="日期"
          width="250"
          sortable
          column-key="time"
          :filters="[{ text: '2018-08-22', value: '2016-05-01' }]"
          :filter-method="filterHandler"
        >
        </el-table-column>

        <el-table-column
          prop="operation"
          label="操作"
          show-overflow-tooltip
          width="250"
          >    <template slot-scope="scope">
  <el-button type="primary" icon="el-icon-edit" circle  @click="changeInfo(scope.row.id)"></el-button
          ><el-button type="danger" icon="el-icon-delete" circle  @click="open(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
      <!-- 添加信息 -->
      <PermissionsApiadd :showdialog.sync="dialogFormVisible"></PermissionsApiadd>
    </div>

  </div>
</template>

<script>
import { list, simple, remove } from '@/api/base/permissions.js'
import PermissionsApiadd from '@/module-manage/components/permissionsApi-add'
export default {
  components: {
    PermissionsApiadd
  },
  data () {
    return {
      dialogFormVisible: false,
      // 分页
      info: {
        page: 1,
        pagesize: 10,
        keyword: '',
        disabled: null
      },
      currentPage4: 1,

      input: '',
      tableData: [
        {
          create_date: '2018-08-22',
          title: '超级管理员组'
        }
      ],
      total: 2,
      // 添加页面
      // dialogFormVisible: false
      text: '添加权限组'

    }
  },

  async created () {
    this.renderList()
  },

  methods: {
    // 渲染页面
    async renderList () {
      const { data } = await list(this.info)
      // console.log(data)
      this.total = data.counts
      this.tableData = data.list
    },
    // 分页
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.info.pagesize = val
      this.info.page = 1
      this.getRenderList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.info.page = val
      this.getRenderList()
    },
    // 渲染
    async getRenderList () {
      const { data } = await list(this.info)
      this.total = data.counts
      this.tableData = data.list
      this.info.page = 1
      this.info.pagesize = 10
    },
    filterHandler (value, row, column) {
      const property = column.property
      return row[property] === value
    },
    // 搜索和清空按钮
    selectPermissions () {
      this.tableData = this.tableData.filter(item => item.title === this.input)
    },
    clearselect () {
      this.input = ''
      this.renderList()
    },
    // 添加权限组
    openAdd () {
      this.dialogFormVisible = true
    },
    handleCloseModal () {
      this.dialogFormVisible = false
    },
    // 两个按钮
    async changeInfo (id) {
      this.dialogFormVisiblechange = true
      const data = await simple(id)
      const a = data.data.filter(item => item.id === id)
      this.form.id = id
      this.form.email = a[0].email
      this.form.phone = a[0].phone
      this.form.username = a[0].username
      this.form.permission_group_id = 1
    },
    open (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.id = id
        remove(this.form)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getRenderList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.all {
  padding: 20px;
  .main {
    padding: 20px;
    background-color: #fff;
  }
  .separate {
    margin: 20px 0;
    width: 100%;
    height: 30px;
    background-color: #ccc;
    padding-left: 15px;
    border-radius: 5px;
    p {
      font-size: 18px;
      line-height: 30px;
    }
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
